<!-- 部门管理页面 -->
<template>
  <div>
    <!-- 根据参数查询 和 添加数据 -->
    <el-row>
      <el-col :span="6">
        <el-input
          v-model="searchForm.commodityName"
          size="mini"
          placeholder="请输入类型"></el-input>
      </el-col>
      <el-col :span="6"></el-col>
      <el-col :span="6">
        <el-button
          type="success"
          size="mini"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        >
        <el-button
          type="primary"
          size="mini"
          icon="el-icon-plus"
          @click="handleAdd"
          >添加</el-button
        >
        <el-button
          type="primary"
          size="mini"
          icon="el-icon-refresh"
          @click="refresh"
          >刷新</el-button
        >
      </el-col>
      <el-col :span="6"></el-col>
    </el-row>
    <!--    查询-->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column label="图片地址" width="160" height="120">
        <template slot-scope="scope">
          <el-image
            style="width: 150px; height: 115px"
            :src="scope.row.imageurl"></el-image>
        </template>
      </el-table-column>

      <el-table-column
        width="120"
        prop="commodityName"
        label="商品名称"></el-table-column>
      <el-table-column
        width="120"
        prop="commodityType"
        label="商品类型"></el-table-column>
      <el-table-column
        width="120"
        align="center"
        prop="integral"
        label="积分"></el-table-column>
      <el-table-column
        width="120"
        prop="price"
        label="价格"></el-table-column>
      <el-table-column
        width="120"
        prop="coupon"
        label="优惠卷"></el-table-column>
      <el-table-column
        width="120"
        prop="freight"
        label="运费"></el-table-column>
      <el-table-column
        width="120"
        prop="stName"
        label="门店"></el-table-column>
      <el-table-column
        width="120"
        prop="prompt"
        label="温馨提示"></el-table-column>
      <el-table-column
        width="120"
        prop="createBy"
        label="创建人"></el-table-column>
      <el-table-column
        width="120"
        prop="createTime"
        label="创建时间"></el-table-column>
      <el-table-column
        width="120"
        prop="updateBy"
        label="修改人"></el-table-column>
      <el-table-column
        width="120"
        prop="updateTime"
        label="修改时间"></el-table-column>
      <!-- 操作设置 -->
      <el-table-column fixed="right" width="210" label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="handleEdit(scope.$index, scope.row)"
            >编辑
          </el-button>
          <el-button type="danger" @click="deleteDept(scope.row.commodityId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页功能 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[3, 6, 12, 24, 120]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!--    添加修改弹窗-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form" ref="form">
        <el-form-item label="商品名称" :label-width="formLabelWidth">
          <el-input
            v-model="form.commodityName"
            autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="商品图片" :label-width="formLabelWidth">
          <template>
            <el-select v-model="form.pictureid" placeholder="商品图片连接">
              <el-option
                v-for="item in options1"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </template>
        </el-form-item>


        <el-form-item label="商品类型" :label-width="formLabelWidth">
          <template>
            <el-select v-model="form.producttypeid" placeholder="商品类型">
              <el-option
                v-for="item in options2"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </template>
        </el-form-item>

        <el-form-item label="门店" :label-width="formLabelWidth">
          <template>
            <el-select v-model="form.storeId" placeholder="门店">
              <el-option
                v-for="item in options3"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </template>
        </el-form-item>

        <el-form-item label="积分" :label-width="formLabelWidth">
          <el-input v-model="form.integral" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="价格" :label-width="formLabelWidth">
          <el-input v-model="form.price" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="优惠卷" :label-width="formLabelWidth">
          <el-input v-model="form.coupon" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="运费" :label-width="formLabelWidth">
          <el-input v-model="form.freight" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="温馨提示" :label-width="formLabelWidth">
          <el-input v-model="form.prompt" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  //数据
  data() {
    return {
      options1:[],
      options2: [],
      options3: [],
      //table的表格数据
      tableData: [],
      currentPage: 1, //页码
      pageSize: 3, //每页显示的数量
      total: 100, //分页总数量
      searchForm: { title: "" }, //查询携带的参数
      dialogFormVisible: false, //使弹出不显示
      title: "关于添加", //title 默认为 攻略添加
      //添加或者更新数据时 携带的参数
      form: {},
      //弹出框的宽度
      formLabelWidth: "120px",
      imageUrl: "",
    };
  },
  //初始化数据
  created() {
    this.loadData();
    this.queryType();
    this.queryImage();
    this.queryStor();
  },
  methods: {
    loadData() {
      //不是直接使用axios 而是使用$http
      //baseURL="http://localhost:8080"
      var _this = this;
      this.$http
        .post("/tbCommodity/queryAboutCommodity", {
          pageNo: this.currentPage,
          pageSize: this.pageSize,
          data: this.searchForm,
        })
        .then(function (response) {
          console.log(response);

          if (response.data.code == 200) {
            //this不可以直接使用 代表的是axios对象
            _this.tableData = response.data.data.records;
            _this.total = response.data.data.total;
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    //查询商品类型
    queryType() {
      let _this = this;
      //使用axios向后台发请求
      this.$http
        .post("/commodityType/queryPage", {
          pageNo: 1,
          pageSize: 300,
        })
        .then(function (response) {
          if (response.data.code == 200) {
            let list = response.data.data.records;
            for (let i = 0; i < list.length; i++) {
              let a = {
                value: list[i].commentId,
                label: list[i].name,
              };
              _this.options2.push(a);
            }
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    //查询图片
    queryImage() {
      let _this = this;
      //使用axios向后台发请求
      this.$http
        .post("/goodsImage/queryPage", {
          pageNo: 1,
          pageSize: 300,
        })
        .then(function (response) {
          if (response.data.code == 200) {
            let list = response.data.data.records;
            for (let i = 0; i < list.length; i++) {
              let a = {
                value: list[i].id,
                label: list[i].imageurl,
              };
              _this.options1.push(a);
            }
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    //查询门店
    queryStor() {
      let _this = this;
      //使用axios向后台发请求
      this.$http
        .post("/tbStore/queryPage", {
          pageNo: 1,
          pageSize: 300,
        })
        .then(function (response) {
          if (response.data.code == 200) {
            let list = response.data.data.records;
            for (let i = 0; i < list.length; i++) {
              let a = {
                value: list[i].storeId,
                label: list[i].storeName,
              };
              _this.options3.push(a);
            }
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    //实行每页有多少条的方法
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.loadData();
    },
    //实现跳转页的方法
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.loadData();
    },
    //刷新
    refresh() {
      //原始js重新请求
      location.reload();
    },
    //查询方法
    search() {
      this.loadData();
      //搜索时,使页码为1
      this.currentPage = 1;
    },
    //更新的方法
    handleEdit(index, row) {
      console.log(index, row);
      //使弹出框显示
      this.dialogFormVisible = true;
      //给用户赋值
      this.title = "更新";
      //点击查询携带数据
      this.form = row;
      this.imageUrl = row.imageurl;
    },
    //添加数据方法
    handleAdd() {
      //使弹出框显示
      this.dialogFormVisible = true;
      //给用户赋值
      this.title = "添加";
      //点击查询携带数据
      this.form = {};
      this.imageUrl = "";
    },
    //关闭方法
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    //删除方法
    deleteDept(id) {
      this.$confirm("此操作将删除该关于, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var _this = this;
          this.$http
            .delete("/tbCommodity/deleteById?id=" + id)
            .then(function (response) {
              console.log(response);
              if (response.data.code == 200) {
                _this.$message({
                  message: "操作成功",
                  type: "success",
                });
                _this.dialogFormVisible = false;
                _this.loadData();
              } else {
                _this.$message("操作失败");
              }
            })
            .catch(function (error) {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //实现用户添加或者更新
    addOrUpdate() {
      var uri = "/tbCommodity/add";
      if (this.title == "更新") {
        uri = "/tbCommodity/update";
      }
      var _this = this;
      this.$http
        .post(uri, this.form)
        .then(function (response) {
          console.log(response);
          if (response.data.code == 200) {
            _this.$message({
              message: "操作成功",
              type: "success",
            });
            _this.dialogFormVisible = false;
            _this.loadData();
          } else {
            _this.$message("操作失败");
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    }, 
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>